package cn.xiaomubiao.oschine.adapter.news_adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.xiaomubiao.oschine.R;
import cn.xiaomubiao.oschine.base.BaseRecyclerViewAdapter;
import cn.xiaomubiao.oschine.bean.News;
import cn.xiaomubiao.oschine.util.SpUtils;
import cn.xiaomubiao.oschine.util.UIUtils;

/**
 * 资讯/热点 列表适配器
 *
 * @author 余伟舟
 */

public class NewsAdapter extends BaseRecyclerViewAdapter {

    List<News> mStringList = new ArrayList<>();

    public NewsAdapter(List<News> stringList) {
        mStringList = stringList;
    }

    @Override
    public ViewHoders onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(UIUtils.getContext(), R.layout.news_adapter_title, null);
        ViewHoders hoders = new ViewHoders(view); //mItemClickListener
        return hoders;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        ViewHoders holders = (ViewHoders) holder;
        News news = mStringList.get(position);
        holders.mTitle.setText(news.getTitle());

        int id = news.getId();

        boolean isLinsent = SpUtils.getBoolean(UIUtils.getContext(), "isLinsent" + id);
        if (isLinsent) {
            // 如果点击过（true）设置为灰色
            holders.mTitle.setTextColor(Color.parseColor("#838383"));

        } else {
            // false
            holders.mTitle.setTextColor(Color.parseColor("#161616"));

        }

        holders.mContent.setText(news.getBody());
        holders.mName.setText(news.getAuthor());
        holders.mViewById.setText(news.getPubDate());
        holders.mId.setText(news.getCommentCount() + "");
    }

    @Override
    public int getItemCount() {
        return mStringList.size();

    }


    public class ViewHoders extends RecyclerView.ViewHolder {

        private TextView mTitle;
        private TextView mContent;
        private TextView mName;
        private TextView mViewById;
        private TextView mId;

        //将全局的监听赋值给接口
        public ViewHoders(View itemView) {
            super(itemView);
            mTitle = (TextView) itemView.findViewById(R.id.title);
            mContent = (TextView) itemView.findViewById(R.id.content);
            mName = (TextView) itemView.findViewById(R.id.name);
            mViewById = (TextView) itemView.findViewById(R.id.time);
            mId = (TextView) itemView.findViewById(R.id.siez);
        }
    }
}
